package com.e.dianshang.controller;

import com.e.dianshang.pojo.Order;
import com.e.dianshang.service.OrderService;
import com.e.dianshang.utils.BusinessException;
import com.e.dianshang.utils.JsonResult;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author Tian同學
 * @Date 2020/11/9 17:58
 * @Version 1.0
 */
@RestController
@RequestMapping("/shoppingCar")
@Api(value = "购物车",tags = "购物车的api")
public class OrderController {

    @Resource
    OrderService orderService;

    @GetMapping("/getList")
    @ApiOperation(value = "购物车列表接口",httpMethod = "GET")
    @ApiImplicitParam(name = "openid",value = "用户微信ID",required = true,dataType = "string",paramType = "query")
    public Map getList(@RequestParam("openid") String openid) throws BusinessException {

        JsonResult jsonResult=new JsonResult();

        List<Map> list = orderService.getList(openid);

        if (!list.isEmpty()){
            jsonResult.setCode("0");
            jsonResult.setData(list);
        }else {
            throw new BusinessException("你还没有添加商品哦");
        }

        return jsonResult.getValues();

    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除购物车商品",httpMethod = "POST")
    public Map delete(@RequestBody @ApiParam(value = "删除商品的pojo信息 数组",required = true) Map<String, List> id) throws BusinessException {

        List ids = id.get("id");
        System.out.println(id);

        JsonResult jsonResult=new JsonResult();

        int delete = orderService.deleteBatch(ids);

        if (delete>0){
            jsonResult.setCode("0");
            jsonResult.setMsg("删除成功！！");
        }else {
            throw new BusinessException("删除失败！！");
        }

        return jsonResult.getValues();

    }

    @PostMapping("/updateQuantity")
    @ApiOperation(value = "修改购物车商品件数",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "购物车商品id",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "quantity",value = "修改商品数量 +为固定1，-为固定-1",required = true,dataType = "int",paramType = "query")
    })
    public Map getTktByName(@RequestParam("id") Integer id,@RequestParam("quantity") Integer quantity) throws BusinessException {

        JsonResult jsonResult=new JsonResult();
        Order select = orderService.select(id);
        Integer quantity1 = select.getQuantity();
        Order order =new Order();
        order.setId(id);
        Integer quantitys = (quantity1+quantity);
        if (quantitys<1){
            throw new BusinessException("至少一件商品哟~~");
        }
        order.setQuantity(quantitys);
        int i = orderService.updateQuantity(order);

        if (i==1){
            jsonResult.setCode("0");
        }else {
            throw new BusinessException("修改失败");
        }

        return jsonResult.getValues();

    }

    @PostMapping("/updateGuiGe")
    @ApiOperation(value = "修改已加入购物车商品规格信息",httpMethod = "POST")
    public Map updateGuiGe(@RequestBody @ApiParam(value = "修改规格属性的json信息 数组",required = true) Order order) throws BusinessException {


        JsonResult jsonResult=new JsonResult();

        int i = orderService.updateGuiGe(order);

        if (i>0){
            jsonResult.setCode("0");
            jsonResult.setMsg("选择成功！！");
        }else {
            throw new BusinessException("选择失败！！");
        }

        return jsonResult.getValues();

    }

}
